package com.vonchange.utils.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import jodd.util.PropertiesUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import com.vonchange.utils.convert.ConvertUtils;
import com.vonchange.utils.string.StringUtil;

/**
 * 
 * 
 * @author wang-zh.neu
 * 
 */
public final class SystemConfig implements InitializingBean {
	private static Log log = LogFactory.getLog(SystemConfig.class);

	/**
	 * 配置文件属性键值对
	 */
	public static Map<String, String> systemProperties = new HashMap<String, String>();
	public static Map<String, Object> sysConfigToModelMap=new HashMap<String, Object>();
	// 资源文件位置
	private Resource[] resources;
	public static  Resource[] resourcesTemp;

	public Resource[] getResources() {
		return resources;
	}

	public void setResources(Resource[] resources) {
		this.resources = resources;
		resourcesTemp=resources;
	}

    private static  void initSystemPropertiesToModel() {
    	for (Entry<String, String> entry : systemProperties.entrySet()) {
    		sysConfigToModelMap.put(entry.getKey().replaceAll("\\.", "_"), entry.getValue());
		}
	}
	/**
	 * 根据key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperty(final String key) {
		return systemProperties.get(key);
	}

	/**
	 * 根据key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static String getTrimProperty(final String key) {
		return StringUtil.trimToEmpty(getProperty(key));
	}

	/**
	 * 根据key获取value,获取为空则返回默认值
	 * 
	 * @param key
	 * @param defaultString
	 * @return
	 */
	public static String getProperty(final String key, final String defaultString) {
		String prop = getProperty(key);
		return StringUtil.isBlank(prop) ? defaultString : prop;
	}

	/**
	 * 根据key获取Integer类型的value
	 * 
	 * @param key
	 *            参数名称.
	 * @return 参数值.
	 */
	public static int getIntProperty(final String key) {
		return getIntProperty(key, 0);
	}

	/**
	 * 根据key获取Integer类型的value,获取为空则返回默认值
	 * 
	 * @param key
	 *            参数名称.
	 * @param defaultValue
	 *            默认值.
	 * @return 参数值.
	 */
	public static int getIntProperty(final String key, int defaultValue) {
		String value = getProperty(key);
		if (null != value) {			
			return ConvertUtils.toInteger(value);		
		} 
	    return defaultValue;
	}

	/**
	 * 根据key获取Long类型的value,获取为空则返回默认值
	 * 
	 * @param key
	 *            参数名称.
	 * @param defaultValue
	 *            默认值.
	 * @return 参数值.
	 */
	public static long getLongProperty(final String key, long defaultValue) {
		String value = getProperty(key);
		if (null != value) {			
			return ConvertUtils.toLong(value);		
		} 
	    return defaultValue;
	}

	/**
	 * 获取配置文件中的整型参数值.
	 * 
	 * @param key
	 *            参数名称.
	 * @return 整型参数值.
	 */
	public static Integer getIntegerProperty(final String key) {
		return getIntegerProperty(key, null);
	}

	/**
	 * 获取配置文件中的整型参数值.
	 * 
	 * @param key
	 *            参数名称.
	 * @param defaultValue
	 *            缺省值.
	 * @return 整型参数值.
	 */
	public static Integer getIntegerProperty(final String key, final Integer defaultValue) {
		String value = getProperty(key);
		if (null!=value) {
			return ConvertUtils.toInteger(value);		
		}
		return defaultValue;
	}

	/**
	 * 获取配置文件中的布尔参数值.
	 * 
	 * @param key
	 *            参数名称.
	 * @return 布尔参数值.
	 */
	public static boolean getBooleanProperty(final String key) {
		return getBooleanProperty(key, false);
	}

	/**
	 * 获取配置文件中的布尔参数.如果文件中没有该参数就返回defaultValue.
	 * 
	 * @param key
	 *            参数名称.
	 * @param defaultValue
	 *            参数默认值.
	 * @return 布尔参数值.
	 */
	public static boolean getBooleanProperty(final String key, final boolean defaultValue) {
		String value = getProperty(key);
		if (null != value) {			
			return ConvertUtils.toBoolean(value);		
		} 
	    return defaultValue;
	}
	
   public static  void initConfig(){ 
	   log.debug("initialize properties files...");
		if (null == systemProperties) {
			systemProperties = new HashMap<String, String>();
		}
		if (null != resourcesTemp && resourcesTemp.length > 0) {
			for (Resource resource : resourcesTemp) {
				log.debug(resource.getFilename());
				Properties prop = new Properties(); 
				try {
					PropertiesUtil.loadFromFile(prop,resource.getFile() );
				} catch (IOException e) {
					e.printStackTrace();
					continue;
				}
				// 返回Properties中包含的key-value的Set视图
				Set<Entry<Object, Object>> set = prop.entrySet();
				// 返回在此Set中的元素上进行迭代的迭代器
				Iterator<Map.Entry<Object, Object>> it = set.iterator();
				String key = null, value = null;
				// 循环取出key-value
				while (it.hasNext()) {
					Entry<Object, Object> entry = it.next();
					key = String.valueOf(entry.getKey());
					value = String.valueOf(entry.getValue());
					if (StringUtil.isNotBlank(key)) {
						value = StringUtil.isNotBlank(value) ? value : "";
						log.debug(key+":"+value);
						systemProperties.put(key, value);
					}
				}
			}
			initSystemPropertiesToModel();
			//初始化参数
			initOtherParam();
		}
   }
   private static void initOtherParam(){
	   
   }
   private static void initOnceParam(){
   }
	/**
	 * 默认获取classpath下config文件中所有的properties文件
	 * 
	 * @throws Exception
	 */
	public void afterPropertiesSet() throws Exception {	
		initConfig();
		initOnceParam();
	}
	
}
